$(function () {
    var vue = new Vue({
        el: '#app',
        data: {
            // 是否显示模态框
            show: false,
            page: {},
            items: [],
            item: {},
            // 模态框类型
            // 0 新增
            // 1 编辑
            modalType: 0
        },
        mounted: function () {
            this.serach();
        },
        methods: {
            serach: function () {
                var req = {
                    condition: {

                    },
                    page: {
                        pageNum: this.page.pageNum !== undefined ? this.page.pageNum : 1,
                        pageSize: this.page.pageSize !== undefined ? this.page.pageSize : 10
                    }
                };
                $.showloading();
                this.$http.post('attendance/machine/list', req).then((res) => {
                    console.log(res);
                    var rsp = res.data;
                    if (rsp.err !== "0") {
                        bootbox.alert({
                            message: rsp.msg,
                            size: 'small'
                        });
                        return;
                    }

                    this.items = rsp.rows;
                    this.page = rsp.page;
                }, (res) => {
                    bootbox.alert({
                        message: "系统错误",
                        size: 'small'
                    });
                }).finally(function () {
                    $.hideloading();
                });
            },
            showModal: function (id) {
                console.log(id);
                if (undefined === id) {
                    this.item = {};
                    this.modalType = 0;
                    this.show = true;
                    return;
                }

                this.modalType = 1;
                $('.modal-mask').animate({
                    opacity: 1
                }, 300, function () {
                    $(this).show();
                });
                $.showloading();
                this.$http.get('attendance/machine/' + id + '/info').then((res) => {
                    var data = res.data;
                    console.log(res.data);

                    if (data.err === '0') {
                        this.item = data.data;
                    }
                    else {
                        bootbox.alert({
                            message: data.msg,
                            size: 'small'
                        });
                    }

                    this.show = true;
                }, (res) => {
                    bootbox.alert({
                        message: "系统错误",
                        size: 'small'
                    });
                }).finally(function () {
                    $.hideloading();
                });
            },
            closeModal: function () {
                this.show = false;
            },
            add: function () {
                $.showloading();
                this.$http.post('attendance/machine/add', {
                    name: this.item.name,
                    address: this.item.address,
                    remark: this.item.remark === undefined ? '' : this.item.remark
                }).then((res) => {
                    var data = res.data;
                    console.log(data);

                    bootbox.alert({
                        message: data.msg,
                        size: 'small'
                    });
                    this.show = false;
                    this.serach();
                }, (res) => {
                    bootbox.alert({
                        message: "系统错误",
                        size: 'small'
                    });
                }).finally(function () {
                    $.hideloading();
                });
            },
            edit: function () {
                $.showloading();
                this.$http.post('attendance/machine/edit', {
                    id: this.item.id,
                    name: this.item.name,
                    address: this.item.address,
                    remark: this.item.remark === undefined ? '' : this.item.remark
                }).then((res) => {
                    var data = res.data;
                    console.log(data);

                    bootbox.alert({
                        message: data.msg,
                        size: 'small'
                    });

                    if (data.err == '0') {
                        this.show = false;
                        this.serach();
                    }
                }, (res) => {
                    bootbox.alert({
                        message: "系统错误",
                        size: 'small'
                    });
                }).finally(function () {
                    $.hideloading();
                });
            },
            del: function (id) {
                var vue = this;
                bootbox.confirm({
                    message: "确认删除编号为" + id + "的考勤机？",
                    buttons: {
                        confirm: {
                            label: '是的',
                            className: 'btn-success'
                        },
                        cancel: {
                            label: '取消',
                            className: 'btn-danger'
                        }
                    },
                    callback: function (result) {
                        if (false === result) {
                            return;
                        }

                        $.showloading();
                        vue.$http.get('attendance/machine/' + id + '/del').then((res) => {
                            console.log(res.data);
                            bootbox.alert({
                                message: res.data.msg,
                                size: 'small'
                            });
                            vue.serach();
                        }, (res) => {
                            bootbox.alert({
                                message: "系统错误",
                                size: 'small'
                            });
                        }).finally(function () {
                            $.hideloading();
                        });
                        return;
                    }
                });
            },
            translatorStatus: function (status) {
                switch (status) {
                    case 0:
                        return '未连接';
                    case 1:
                        return '连接失败';
                    case 2:
                        return '连接成功';
                    case 3:
                        return '断开连接';
                    case 4:
                        return '连接中';
                    case 5:
                        return '重连中';
                    default:
                        return '未知状态';
                }
            },
            connec: function (id) {
                $.showloading();
                this.$http.post('attendance/machine/connec', {
                    id: id
                }).then((res) => {
                    var data = res.data;
                    console.log(data);

                    if (data.err != '0') {
                        bootbox.alert({
                            message: data.msg,
                            size: 'small'
                        });
                    }
                    this.serach();
                }, (res) => {
                    bootbox.alert({
                        message: "系统错误",
                        size: 'small'
                    });
                }).finally(function () {
                    $.hideloading();
                });
            },
            stop: function (id) {
                $.showloading();
                this.$http.post('attendance/machine/stop', {
                    id: id
                }).then((res) => {
                    var data = res.data;
                    console.log(data);

                    if (data.err != '0') {
                        bootbox.alert({
                            message: data.msg,
                            size: 'small'
                        });
                    }
                    this.serach();
                }, (res) => {
                    bootbox.alert({
                        message: "系统错误",
                        size: 'small'
                    });
                }).finally(function () {
                    $.hideloading();
                });
            }
        }
    });
});